﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using OrderMgr.SysSetting;
using OrderMgr.Rpt;
using OrderMgr.CustomerInfo;
using System.Collections;
using TierApplication;

namespace OrderMgr.Rpt
{
    public partial class frmRptSaleProduct : TierService.frmTemplate
    {

        int iOrderNum = 0, iProductCount = 0;
        decimal dSumMoney = 0;

        public frmRptSaleProduct()
        {
            InitializeComponent();
            this.Text = "商品销售汇总";
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            //Utility.DataToLookupComboxEx(txtOrderStatus, C_DMB.GetListByType(C_DMB.DType.OrderStatus.ToString()), "CodeID", "CodeName");          
            DataTable dtOrderStatus = C_DMB.GetListByType(C_DMB.DType.OrderStatus.ToString()).Tables[0];
            txtOrderStatus.Properties.Items.Clear();
            txtOrderStatus.Properties.SeparatorChar = ';';
            for (int i = 0; i < dtOrderStatus.Rows.Count; i++)
            {
                string sCodeID = dtOrderStatus.Rows[i]["CodeID"].ToString();
                string sCodeName = dtOrderStatus.Rows[i]["CodeName"].ToString();
                if ((sCodeID == "3") || (sCodeID == "4"))
                {
                    txtOrderStatus.Properties.Items.Add(sCodeID, sCodeName, CheckState.Checked, true);
                }
                else
                    txtOrderStatus.Properties.Items.Add(sCodeID, sCodeName, CheckState.Unchecked, true);
            }
            
            
            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtSupplierID, (new T_Supplier()).GetList(""), "SupplierID", "SupplierName");
            Utility.DataToLookupComboxEx(txtProductType, C_DMB.GetListByType(C_DMB.DType.ProductType.ToString(), " and Remark='2'"), "CodeID", "CodeName");

            ckbOrderDate.Checked = true;
            txtOrderDateStart.DateTime = DateTime.Now.AddDays(-7);
            txtOrderDateEnd.DateTime = DateTime.Now.Date;
        }

        private void frmRptSaleProduct_Load(object sender, EventArgs e)
        {
            initForm();

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string ls_where = "";

            ls_where = " and a.OrderType ='" + (int)Utility.OrderType.Sale + "'";

            if ((txtProductName.Text.Trim() != "") && (ckbProductName.Checked))
                ls_where = ls_where + " and a.ProductName  like '%" + txtProductName.Text + "%'";

            if ((txtSupplierID.Text.Trim() != "") && (ckbSupplierID.Checked))
                ls_where = ls_where + " and a.SupplierID = '" + (string)txtSupplierID.EditValue + "'";

            if ((txtOrderID.Text != "") && (ckbOrderID.Checked))
                ls_where = ls_where + " and a.OrderID like '%" + txtOrderID.Text + "%'";

            if ((txtDepotID.Text.Trim() != "") && (ckbDepotID.Checked))
                ls_where = ls_where + " and a.DepotID = '" + (string)txtDepotID.EditValue + "'";
            
            if ((txtOrderStatus.Text.Trim() != "") && (ckbOrderStatus.Checked))
            {
                string sOrderStatus = (string)txtOrderStatus.EditValue;
                string sStatus = sOrderStatus.Replace(';', ',');
                ls_where = ls_where + " and a.OrderStatus in (" + sStatus + ")";
            }

            if ((txtOrderUserID.Text.Trim() != "") && (ckbOrderUserID.Checked))
                ls_where = ls_where + " and a.OrderUserID = '" + (string)txtOrderUserID.EditValue + "'";

            if ((ckbOrderDate.Checked) && (txtOrderDateStart.Text != "") && (txtOrderDateEnd.Text != ""))
            {
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) >='" + txtOrderDateStart.DateTime.ToString("yyyy-MM-dd") + "'";
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) <='" + txtOrderDateEnd.DateTime.ToString("yyyy-MM-dd") + "'";
            }

            if ((txtCustID.Text.Trim() != "") && (ckbCustID.Checked))
                ls_where = ls_where + " and a.CustID = '" + txtCustID.Text + "'";

            T_Order mOrder = new T_Order();
            DataSet dsCustomer = mOrder.GetSaleProductSumList(ls_where);
            gcOrder.DataSource = dsCustomer.Tables[0];
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            TierService.Utility.DataExportToExcel(gvOrder, "订单信息");
        }



        private void gvOrder_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            //if (gvOrder.RowCount < 1) return;
            //DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            //string sID = dr["OrderID"].ToString();

            //frmRptSaleDetail fDetail = new frmRptSaleDetail();
            //fDetail.OpMode = TierService.Utility.OperateMode.Query;
            //fDetail._OrderStatus = Utility.OrderStatus.Input;
            //fDetail._OrderType = Utility.OrderType.Sale;
            //fDetail._OrderID = sID;
            //fDetail.ShowDialog();
        }

        

        private void gvOrder_DoubleClick(object sender, EventArgs e)
        {
            btnQuery_Click(null, null);
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtCustID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmCustomer fDetail = new frmCustomer();
            fDetail.gFuncType = Utility.FuncType.Select;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtCustID.Text = fDetail._CustID;
            }
        }

    }

}